#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<string> maze(n);
  for (auto &s : maze) cin >> s;
  struct State {
    int x, y, up, down, left, right;
    bool operator == (const State &rhs) const {
      return x == rhs.x && y == rhs.y &&
        up == rhs.up && down == rhs.down &&
        left == rhs.left && right == rhs.right;
    }
  };
  struct Hash {
    size_t operator () (const State &s) const {
      return (s.x + 12) | ((s.y + 12) << 6) |
        ((s.up + 12) << 12) | ((s.down + 12) << 18) |
        ((s.left + 12) << 24) | ((size_t)(s.right + 12) << 30);
    }
  };
  unordered_set<State, Hash> vis;
  queue<State> q;
  int sx = 0, sy = 0;
  for (int i = 0; i < n; i ++) {
    for (int j = 0; j < m; j ++) {
      if (maze[i][j] == 'T') {
        sx = i, sy = j;
        q.emplace(i, j, 0, n - 1, 0, m - 1);
        vis.insert(q.back());
      }
    }
  }
  int step = 0;
  vector<pair<int, int>> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  while (!q.empty()) {
    int sz = q.size();
    while (sz --) {
      auto [x, y, up, down, left, right] = q.front();
      q.pop();
      int cnt = 0;
      for (int i = up; i <= down; i ++) {
        for (int j = left; j <= right; j ++) {
          if (maze[i][j] == '#') cnt ++;
        }
      }
      if (cnt == 0) {
        cout << step << endl;
        return;
      }
      for (auto [dx, dy] : dir) {
        int nx = x + dx, ny = y + dy;
        if (nx < -n || nx >= 2 * n || ny < -m || ny >= 2 * m) continue;
        if (nx >= up && nx <= down &&
            ny >= left && ny <= right &&
            maze[nx][ny] == '#') continue;
        int nup = max(up, nx - sx);
        int ndown = min(down, nx - sx + n - 1);
        int nleft = max(left, ny - sy);
        int nright = min(right, ny - sy + m - 1);
        State nst = {nx, ny, nup, ndown, nleft, nright};
        if (vis.count(nst)) continue;
        vis.insert(nst);
        q.emplace(nst);
      }
    }
    step ++;
  }
  cout << -1 << endl;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T = 1;
  // cin >> T;
  for (int cas = 1; cas <= T; cas ++) {
    // cout << "Case #" << cas << ": ";
    solve();
  }
}